package com.pg.service.impl;


import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.postgresql.PGConnection;
import org.postgresql.largeobject.LargeObject;
import org.postgresql.largeobject.LargeObjectManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pg.entity.sy.EipMechanicalCharacteristics;
import com.pg.mapper.EipMechanicalCharacteristicsMapper;
import com.pg.service.EipMechanicalCharacteristicsService;

/**
 * <p>
 * 机械特性 服务实现类
 * </p>
 *
 * @Author likl
 * @since 2020-06-07
 */
@Service
@DS("proxyDs")
@Primary
public class EipMechanicalCharacteristicsServiceImpl extends ServiceImpl<EipMechanicalCharacteristicsMapper, EipMechanicalCharacteristics> implements EipMechanicalCharacteristicsService {

    @Autowired
    private EipMechanicalCharacteristicsMapper eipMechanicalCharacteristicsMapper;

    @Autowired
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    @Override
    public void getRoutePicture(HttpServletResponse response, String loid) throws Exception {
        Connection connection = null;
        LargeObject largeObject = null;
        OutputStream outputStream = null;
        try {
            connection = dynamicRoutingDataSource.getCurrentDataSources().get("proxyDs").getConnection();
            connection.setAutoCommit(false);
            LargeObjectManager largeObjectManager = connection.unwrap(PGConnection.class).getLargeObjectAPI();
            Long oid = Long.parseLong(loid);
            largeObject = largeObjectManager.open(oid, LargeObjectManager.READ);
            int LgeObjLength = largeObject.size();
            byte[] data = new byte[LgeObjLength];
            response.setContentLength(LgeObjLength);
            largeObject.read(data, 0, LgeObjLength);
            outputStream = response.getOutputStream();
            outputStream.write(data);
            outputStream.flush();
            largeObject.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            connection.close();
            largeObject.close();
            outputStream.close();
        }
    }

    @Override
    public IPage<EipMechanicalCharacteristics> selectForList(Page<EipMechanicalCharacteristics> page, EipMechanicalCharacteristics eipMechanicalCharacteristics, Date startCheckTime, Date endCheckTime) {
        LambdaQueryWrapper<EipMechanicalCharacteristics> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(eipMechanicalCharacteristics.getSupplierWorkNo())) {
            queryWrapper.like(EipMechanicalCharacteristics::getSupplierWorkNo, eipMechanicalCharacteristics.getSupplierWorkNo());
        }
        if (StringUtils.isNotEmpty(eipMechanicalCharacteristics.getCategoryType())) {
            queryWrapper.like(EipMechanicalCharacteristics::getCategoryType, eipMechanicalCharacteristics.getCategoryType());
        }
        if (StringUtils.isNotEmpty(eipMechanicalCharacteristics.getProductModel())) {
            queryWrapper.like(EipMechanicalCharacteristics::getProductModel, eipMechanicalCharacteristics.getProductModel());
        }
        if (StringUtils.isNotEmpty(eipMechanicalCharacteristics.getEquipmentName())) {
            queryWrapper.like(EipMechanicalCharacteristics::getEquipmentName, eipMechanicalCharacteristics.getEquipmentName());
        }
        if (StringUtils.isNotEmpty(eipMechanicalCharacteristics.getPdCode())) {
            queryWrapper.like(EipMechanicalCharacteristics::getPdCode, eipMechanicalCharacteristics.getPdCode());
        }
        if (startCheckTime != null) {
            queryWrapper.ge(EipMechanicalCharacteristics::getCheckTime, startCheckTime);
        }
        if (endCheckTime != null) {
            queryWrapper.le(EipMechanicalCharacteristics::getCheckTime, endCheckTime);
        }
        return eipMechanicalCharacteristicsMapper.selectPage(page, queryWrapper);
    }
}
